package com.taxi.management.controller;

import com.taxi.management.entity.OrderDetailsEntity;
import com.taxi.management.entity.ResultEntity;
import com.taxi.management.service.OrderDetailsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v3/orderDetail/")
@CrossOrigin//解决跨域问题
@RestController//Json
public class OrderDetailController {
    @Resource
    OrderDetailsService orderDetailsService;

    @GetMapping("getAllOrder")
    public ResultEntity getAllOrder() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(orderDetailsService.getAllOrder());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(orderDetailsService.getAllOrder().size());
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getOrderById")
    public ResultEntity getOrderById(@RequestParam int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(orderDetailsService.getOrderById(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PostMapping("addOrder")
    public ResultEntity addOrder(@RequestBody OrderDetailsEntity order) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(orderDetailsService.addOrder(order));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PutMapping("updateOrder")
    public ResultEntity updateOrder(@RequestBody OrderDetailsEntity order) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(orderDetailsService.updateOrder(order));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @DeleteMapping("deleteOrder/{id}")
    public ResultEntity deleteOrder(@PathVariable int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(orderDetailsService.deleteOrder(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getOrderByPage")
    public ResultEntity getOrderByPage(@RequestParam String username, @RequestParam int page, @RequestParam int limit) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            int start=(page-1)*limit;
            int cnt=-1;
            List<OrderDetailsEntity> list = orderDetailsService.getOrderByPage(username,start,limit);
            if(!username.equals("")){
                cnt=list.size();//有查询条件的时候，就按查询回来的条数计
            }else{
                cnt= orderDetailsService.getAllOrder().size();//当没有查询条件的时候 就获取所有用户数量
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(cnt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
}
